# -*- coding: utf-8 -*-
"""
Created on Sat Dec  5 15:13:55 2020
Select
@author: andrew
"""

import time
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
# create webdriver object 
driver = webdriver.Chrome('./diver/chromedriver')
driver.get("https://docs.python.org/3/")
time.sleep(2)

# <span class="language_switcher_placeholder">
#     <select>
#         <option value="en" selected="selected">English</option>
#         <option value="es">Spanish</option>
#         <option value="fr">French</option>
#         <option value="ja">Japanese</option>
#         <option value="ko">Korean</option>
#         <option value="pt-br">Brazilian Portuguese</option>
#         <option value="zh-cn">Simplified Chinese</option>
#         <option value="zh-tw">Traditional Chinese</option>
#     </select>
# </span>
try:
    #1 通过option老式方法来操作select
    # element = driver.find_element_by_xpath("/html/body/div[1]/ul/li[5]/span[1]/select")
    # all_options = element.find_elements_by_tag_name("option")
    # for option in all_options:
    #     print("Text is: ", option.get_attribute("innerHTML")," | ","Value is: ", option.get_attribute("value"))
    #     print(driver.current_url)
    #     option.click()


    #2 通过Select方法来操作select中的option
    # select = Select(driver.find_element(By.XPATH,"/html/body/div[1]/ul/li[5]/span[1]/select"))
    # # # select.select_by_index(3)
    # # # select.select_by_visible_text("French")
    # # # select.select_by_value('fr')
    # options = select.options
    # for opt in options:
    #     print(opt.get_attribute("innerHTML"))
    #     opt.click()
    #     time.sleep(1)

    #3 通过Select遍历option，然后将其选择运行，相当于clock
    select = Select(driver.find_element(By.XPATH,"/html/body/div[1]/ul/li[5]/span[1]/select"))
    for index in range(len(select.options)):
        select.select_by_index(index)
        time.sleep(1)
        # select = Select(driver.find_element(By.XPATH,"/html/body/div[1]/ul/li[5]/span[1]/select"))
except Exception as e:
    print(e)
finally:
    time.sleep(5)
    driver.quit()


